﻿using System;
using System.Data.SqlClient;
using System.Linq;
using AB.ServiceDirectory.Commons.DataFilters;
using AB.ServiceDirectory.Entities;

namespace AB.ServiceDirectory.Dao
{
    [Serializable]
    public class HServiceOrganizationDao
    {
        [NonSerialized] private readonly ServicesDirectoryDataContext _context = DB.Context;


        /// <summary>
        /// Gets the list.
        /// </summary>
        /// <param name="filter">The filter.</param>
        /// <param name="sortType">Type of the sort.</param>
        /// <param name="startRowIndex">Start index of the row.</param>
        /// <param name="max">The max.</param>
        /// <returns></returns>
        public IQueryable<ServiceOrganization> GetList(HServiceOrganizationFilter filter, string sortType,
                                                       int startRowIndex, int max)
        {
            IQueryable<ServiceOrganization> data = from so in _context.ServiceOrganizations
                                                   select so;

            //filter
            //if (filter!=null)
            //{
            data = GetListQuery(data, filter);
            //}

            //sort
            data = GetListSort(data, sortType);

            //Limit the result
            data = data.Skip(startRowIndex).Take(max);

            //return Data
            return data;
        }

        /// <summary>
        /// Gets the list sort.
        /// </summary>
        /// <param name="data">The data.</param>
        /// <param name="sortType">Type of the sort.</param>
        /// <returns></returns>
        private static IQueryable<ServiceOrganization> GetListSort(IQueryable<ServiceOrganization> data, string sortType)
        {
            bool sortDecending = false;
            if (!string.IsNullOrEmpty(sortType))
            {
                string[] values = sortType.Split((' '));
                sortType = values[0];
                if (values.Length > 1)
                {
                    sortDecending = (values[1] == "DESC");
                }

                switch (sortType)
                {
                    case "OrganizationName":
                        data = sortDecending
                                   ? data.OrderByDescending(so => so.Organization.OrganizationName)
                                   : data.OrderBy(so => so.Organization.OrganizationName);
                        break;
                    case "Description":
                        data = sortDecending
                                   ? data.OrderByDescending(so => so.Description)
                                   : data.OrderBy(so => so.Description);
                        break;
                    case "Roles":
                        data = sortDecending ? data.OrderByDescending(so => so.Roles) : data.OrderBy(so => so.Roles);
                        break;
                }
            }
            return data;
        }

        /// <summary>
        /// Gets the list query.
        /// </summary>
        /// <param name="data">The data.</param>
        /// <param name="filter">The filter.</param>
        /// <returns></returns>
        private static IQueryable<ServiceOrganization> GetListQuery(IQueryable<ServiceOrganization> data,
                                                                    HServiceOrganizationFilter filter)
        {
            //Filter by ServiceId
            data = data.Where(so => so.ServiceId == filter.ServiceId && so.Organization.IsActive);
            return data;
        }

        /// <summary>
        /// Gets the list count.
        /// </summary>
        /// <param name="filter">The filter.</param>
        /// <returns></returns>
        public int GetListCount(HServiceOrganizationFilter filter)
        {
            IQueryable<ServiceOrganization> data = from so in _context.ServiceOrganizations
                                                   select so;
            if (filter != null)
            {
                data = GetListQuery(data, filter);
            }
            return data.Count();
        }

        /// <summary>
        /// Updates the service organization role.
        /// </summary>
        /// <param name="serviceId">The service id.</param>
        /// <param name="organizationId">The organisation id.</param>
        /// <param name="roles">The roles.</param>
        /// <returns></returns>
        public bool UpdateServiceOrganizationRole(int serviceId, int organizationId, string roles)
        {
            bool result;
            ServiceOrganization serviceOrganization =
                _context.ServiceOrganizations.FirstOrDefault(
                    s => s.ServiceId == serviceId && s.OrganizationId == organizationId);
            if (serviceOrganization == null)
            {
                result = false;
            }
            else
            {
                try
                {
                    serviceOrganization.Roles = roles;
                    _context.SubmitChanges();
                    result = true;
                }
                catch (SqlException e)
                {
                    Log.GetLog(e);
                    result = false;
                }
            }
            return result;
        }
    }
}
